﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Audit_Configuration_Oracle.Controllers;

namespace Audit_Configuration_Oracle.Views
{
    public partial class ACO_Audit_Rules_Delete : Form
    {
        List<Dictionary<string, string>> _row = null;

        public ACO_Audit_Rules_Delete()
        {
            InitializeComponent();
        }

        public ACO_Audit_Rules_Delete(List<Dictionary<string, string>> selectedRow, int rulesStart)
        {
            InitializeComponent();
            _row = selectedRow;

            SetGeneralData(rulesStart);
            CreateCheckRules(rulesStart);
        }
        private void SetGeneralData(int rulesStart)
        {
            Dictionary<string, string> record = null;
            for (int i = 1; i < rulesStart; i++)
            {
                record = _row[i];
                switch (record["column"])
                {
                    case "PROPIETARIO":
                        lblOwner.Text = record["value"];
                        break;
                    case "NOMBRE OBJETO":
                        lblName.Text = record["value"];
                        break;
                    case "TIPO OBJETO":
                        lblType.Text = record["value"];
                        break;                    
                    default:
                        break;
                }
            }
        }
        private void CreateCheckRules(int rulesStart)
        {
            Dictionary<string, string> record = null;
            string colName = string.Empty;
            string value = string.Empty;
            string optionToAdd = string.Empty;

            for (int i = rulesStart; i < _row.Count; i++)
            {
                record = _row[i];
                colName = record["column"];
                value = record["value"];

                if (!value.ToLower().Equals("not set"))
                {
                    colName = colName.Substring(colName.IndexOf(" ")+1);

                    switch (colName.ToUpper())
                    {
                        case "SEL":
                            optionToAdd = "SELECT";
                            break;
                        case "UPD":
                            optionToAdd = "UPDATE";
                            break;
                        case "DEL":
                            optionToAdd = "DELETE";
                            break;
                        case "INS":
                            optionToAdd = "INSERT";
                            break;
                        default:
                            break;
                    }

                    chkListActive.Items.Add(optionToAdd);
                    i++;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> selectedRules = new List<string>();
            string target = string.Empty;             
            try
            {
                for (int x = 0; x <= chkListActive.CheckedItems.Count - 1; x++)
                {
                    selectedRules.Add(chkListActive.CheckedItems[x].ToString());
                }
                target = string.Format("{0}.{1}", lblOwner.Text, lblName.Text);

                if (AuditRulesController.DeactiveObjectAuditRules(target, selectedRules))
                {
                    MessageBox.Show("Transacción exitosa", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Transacción fallida", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            catch (Exception error)
            {
            }
        }

        private void chkListActive_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BeginInvoke(new MethodInvoker(evalList), null);
        }

        private void evalList()
        {
            /*bool any = false;
            for (int ix = 0; ix < chkListActive.Items.Count; ++ix)
            {
                if (chkListActive.GetItemChecked(ix))
                {
                    any = true;
                    break;
                }
            }
            btnDelete.Enabled = any;*/
            btnDelete.Enabled = chkListActive.CheckedItems.Count > 0;
        }
    }
}
